package com.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.model.DeviceService;

/**
 * Servlet implementation class DeviceDeal
 */
@WebServlet("/files/DeviceDeal")
public class DeviceDeal extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private static final String ERROR_PAGE = "DeviceFailed.jsp";
	private static final String SUCCESS_PAGE = "listSB.jsp";
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public DeviceDeal() {
        super();
    }

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		request.setCharacterEncoding("UTF-8");response.setCharacterEncoding("UTF-8");
		String flag=request.getParameter("flag");
		
		switch(flag){
			case "delete":{
				if( !DeviceService.deviceDelete(request) ) {
					response.sendRedirect(ERROR_PAGE);
				}
				else {
					response.sendRedirect(SUCCESS_PAGE);
				}
				break;
			}
			case "export":{
				String excel=DeviceService.deviceExport(request);
				if( excel!=null ) {
					response.sendRedirect("../"+excel);
				}
				else {
					response.sendRedirect(SUCCESS_PAGE);
				}
				break;
			}
			case "import":{
				if( !DeviceService.deviceImport(request) ) {
					response.sendRedirect(ERROR_PAGE);
				}
				else {
					response.sendRedirect(SUCCESS_PAGE);
				}
				break;
			}
			case "scrap":{
				if( !DeviceService.deviceScrap(request) ) {
					response.sendRedirect(ERROR_PAGE);
				}
				else {
					response.sendRedirect(SUCCESS_PAGE);
				}
				break;
			}
			case "scrap_cancel":{
				if( !DeviceService.deviceScrapCancel(request) ) {
					response.sendRedirect(ERROR_PAGE);
				}
				else {
					response.sendRedirect(SUCCESS_PAGE);
				}
				break;
			}
			case "translate":{
				if( !DeviceService.deviceTranslate(request) ) {
					response.sendRedirect(ERROR_PAGE);
				}
				else {
					response.sendRedirect(SUCCESS_PAGE);
				}
				break;
			}
			default:break;
		}
	}

}
